#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
    Created on 2010-4-7

    @author: neatchenheng 
    
            本moduls 将会抓取http://taoke.alimama.com/spreader/search_auction.htm
            页面上的“单品热推商品”
'''

import urllib
import xml.dom.minidom
import os
import time
from beautifulsoup.BeautifulSoup import BeautifulSoup

class SearchHotAuction(object):
    search_url = None
    local_path = None
    hot_auction_file = None
    TIMEFORMAT = "%y-%m-%d %X"
    
    def __init__(self): 
        self.search_url = "http://taoke.alimama.com/spreader/search_auction.htm"
        self.local_path = "/home/chenheng/workspace/python/taokemen/src/resources/xml/"
        self.hot_auction_file = self.local_path + "hot_auctions_category.xml"
        self.makeLocalPath()
        
    def makeLocalPath(self): 
        if not os.path.exists(self.local_path):
            os.makedirs(self.local_path)
    
    def collectHotAuction(self,soup):
        doc = xml.dom.minidom.Document()
        hot_auction_cate = soup.findAll('a',attrs={'href':'#','onclick':'searchBouti(this);return false;'})
        root = doc.createElement("auctions")
        doc.appendChild(root)
        for cate in hot_auction_cate :
            auction = doc.createElement("auction")
            auction.setAttribute("params",cate["params"])
            auction.appendChild(doc.createTextNode(cate.string.encode('utf-8')))
            root.appendChild(auction)
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Collect auction(" + auction.toxml('utf-8') +") succeed"
            
        doc.writexml(open(self.hot_auction_file,"w"), "", "    ", "\n", "utf-8")
        
    def execute(self): 
        print "========================================================================"
        print "==>> 'SearchHotAuction.execute' schedule "
        html = urllib.urlopen(self.search_url)
        content = html.read()
        if not content == None :
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Read resource(" + self.search_url + ") succeed"
        else :
            print "[" + time.strftime(self.TIMEFORMAT,time.localtime()) + "]: Read resource(" + self.search_url + ") failed"
        soup = BeautifulSoup("".join(content))
        self.collectHotAuction(soup)    
        print "========================================================================"
        
           
if __name__ == '__main__':
    search_hot_auction = SearchHotAuction()
    search_hot_auction.execute()
    pass